using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Lucene.Net.Search;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Documents;
using FutureOpen.Core.func.analyzer;
using Lucene.Net.Analysis;

namespace FutureOpen.Core.func.search
{
    public class LuceneSearch
    {
        public static BooleanClause.Occur[] ArtFlags = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
        public static String[] ArtFields = new String[] { FieldNames.FIELD_ART_TITLE, FieldNames.FIELD_ART_CONTENT };
        
        public static Hits SearchByRel(IndexReader reader, Query query)
        {
            return NormalSearch(reader, query, Sort.RELEVANCE);
        }

        public static Hits SearchByRel(IndexReader reader, Query query, Filter filter)
        {
            Searcher searcher = new IndexSearcher(reader);
            return searcher.Search(query, filter, Sort.RELEVANCE);
        }

        public static Hits SearchByTime(IndexReader reader, Query query, Filter filter)
        {
            Sort sort = new Sort(new SortField[] { new SortField(FieldNames.FIELD_ART_TIME, SortField.INT, true) });
            
            Searcher searcher = new IndexSearcher(reader);
            return searcher.Search(query, filter, sort);
        }

        public static Hits SearchByTime(IndexReader reader, Query query)
        {
            Sort sort = new Sort(new SortField[] { new SortField(FieldNames.FIELD_ART_TIME, SortField.INT, true) });
            return NormalSearch(reader, query, sort);
        }

        private static Hits NormalSearch(IndexReader reader, Query query, Sort sort)
        {
            Searcher searcher = new IndexSearcher(reader);
            return searcher.Search(query, sort);
        }

        public static LinkedList<SuggestItem> PostSuggestion(string title, int count)
        {
            IndexReader reader = IndexManager.GetArtIndexReader();
            ShootAnalyzer analyzer = new ShootAnalyzer();

            TokenStream stream = analyzer.TokenStream(FieldNames.FIELD_ART_TITLE, new StringReader(title));
            Lucene.Net.Analysis.Token tok = null;

            StringBuilder sb = new StringBuilder();

            while ((tok = stream.Next()) != null)
            {
                string text = tok.TermText();

                sb.Append(text + " ");
            }

            string newquery = sb.ToString().Trim();
            QueryParser parser = new QueryParser(FieldNames.FIELD_ART_TITLE, analyzer);
            Query query = parser.Parse(newquery);


            LinkedList<SuggestItem> items = new LinkedList<SuggestItem>();

            TopDocCollector coll = SuggestTitle(query, count);
            ScoreDoc[] docs = coll.TopDocs().scoreDocs;


            for (int i = 0, j = 0; j < count && i < docs.Length; j++,i++)
            {
                ScoreDoc doc = docs[i];

                Document document = reader.Document(doc.doc);
                string curTitle = document.GetField(FieldNames.FIELD_ART_TITLE).StringValue();
                int id = Int32.Parse(document.GetField(FieldNames.FIELD_ART_ID).StringValue());

                SuggestItem item = new SuggestItem();
                item.DocID = doc.doc;
                item.Title = curTitle;
                item.DbID = id;

                items.AddLast(item);               
            }                        

            return items;
        }

        public static TopDocCollector SuggestTitle(Query query, int count)
        {
            TopDocCollector hits = new TopDocCollector(count);
            IndexReader contentsIndexReader = IndexManager.GetArtIndexReader();
            Searcher searcher = new IndexSearcher(contentsIndexReader);
            searcher.Search(query, hits);            
            return hits;
        }

    }
}
